package com.sprint.controller;

import ch.qos.logback.core.util.TimeUtil;
import com.alibaba.csp.sentinel.adapter.spring.webmvc.callback.BlockExceptionHandler;
import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.alibaba.csp.sentinel.slots.block.BlockException;
import com.sun.scenario.effect.impl.sw.sse.SSEBlend_SRC_OUTPeer;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.concurrent.TimeUnit;

@RestController
@Slf4j
public class FLowLimitController {

    @GetMapping("/testA")
    public String testA(){
        return  "---------------testA";
    }

    @GetMapping("/testB")
    public String testB(){
        log.info("线程：" + Thread.currentThread().getName() +  "----testB");

        return  "---------------testB";
    }

    @GetMapping("/testD")
    public String testD(){
        try {
            TimeUnit.SECONDS.sleep(1);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        log.info("线程：" + Thread.currentThread().getName() +  "----testD  --RT模式");

        return  "---------------testD";
    }

    @GetMapping("/testHostKey")
    @SentinelResource(value = "testHostKey",blockHandler = "deal_testHostKey")
    public String testHostKey(@RequestParam(value = "p1",required = false) String p1,
                              @RequestParam(value = "p2",required = false) String p2 )
    {
        //必须使用此方法配置，不输入blockHandler会讲错误打到界面上
        return  "---------------testHostKey";
    }
    //热点的兜底方法
    public String deal_testHostKey(String p1, String p2, BlockException exception){
        return "---------------deal_testHostKey ---0_0"; //默认提示： blocked by sentinel （flow limiting）

    }
}
